<?php

class prize_ctrl extends Controller
{

    public function __construct()
    {
        parent::__construct();

    }

    //获取奖品
    public function get_prize()
    {
        if (!$this->ajax_err && $this->uid) {

            $data = array(
                'prize'=>array(),
                'msg'=>array(),
                'allmsg'=>''
            );

            $_mdl = Loader::mdl("prize_mdl");

            $prize_list_day = $_mdl->list_prize_day($this->uid);

            /*array(
                array(
                    'title'=> '积分',
                    'type'=>'jf',
                    'val'=> 100
                ),
                array(
                    'title'=> '优惠券',
                    'type'=>'yhq',
                    'val'=> 5
                )
            )*/

            $prize = array(
                array(
                    'title'=> '优惠券',
                    'type'=>'yhq',
                    'val'=> '10元'
                )
            );

            if(count($prize_list_day) < 5){
                $isget = false;
                foreach ($prize as $val){
                    if($val['type'] == 'jf'){

                        $_mdl = Loader::mdl("code_mdl");
                        $code = $_mdl->get_code($val['type'], $this->uid, $this->nick);

                        if($code['code']){
                            //$_mdl->save_code($val['type'], $code['id'], $this->uid, $this->nick);

                            $pdata = json_encode(
                                array(
                                    "socialNo" => $this->uid,
                                    "point" => $val['val'],
                                    "redeemCode" => $code['code']
                                )
                            );

                            $res = curl_fn($pdata, API_IP.'hwcrmweb/point/givingPointBySocialNoAndRedeemCode.do');

                            $res = json_decode($res,true);
                            //dump($pdata);
                            //dump($res);
                            if($res['rspCode'] == 1) {
                                $isget = true;
                                array_push($data['prize'], $val);
                                array_push($data['msg'], $res['rspMsg']);
                            }else{
                                array_push($data['msg'], $res['rspMsg']);
                            }
                        }else{
                            $data['allmsg'] = '奖品已发放完，下次再来吧。';
                        }
                    }

                    if($val['type'] == 'yhq'){
                        $_mdl = Loader::mdl("code_mdl");
                        $code = $_mdl->get_code($val['type'], $this->uid, $this->nick);

                        if($code['code']){
                            //$_mdl->save_code($val['type'], $code['id'], $this->uid, $this->nick);

                            $pdata = json_encode(
                                array(
                                    "socialNo" => $this->uid,
                                    "redeemCode" => $code['code'],
                                    "count" => 'SKIP'
                                )
                            );

                            $res = curl_fn($pdata, API_IP.'hwcrmweb/coupon/exchangeBySocialNoAndRedeemCode.do');

                            $res = json_decode($res,true);
                            /*dump($code);
                            dump($res);
                            dump($res['rspCode']);
                            dump($res['rspCode'] == 1);*/
                            if($res['rspCode'] == 1) {
                                $isget = true;
                                array_push($data['prize'], $val);
                                array_push($data['msg'], $res['rspMsg']);
                            }else{
                                array_push($data['msg'], $res['rspMsg']);
                            }
                        }else{
                            $data['allmsg'] = '奖品已发放完，下次再来吧。';
                        }

                    }
                }

                if($isget == true) {
                    $_mdl = Loader::mdl("prize_mdl");
                    $_mdl->save_prize($this->uid, $this->nick, json_encode($data['prize']));
                }
            }else{
                $data['allmsg'] = '您已领取过奖励，明日再来~';
            }

            if(count($data['prize']) == 0 && $data['allmsg'] == ''){
                $data['allmsg'] = '奖品已发放完，下次再来吧。';
            }

            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }

}